package com.cognitran.cssvalidator.ant.taskdefs;

import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * The main CSS validation ant task.
 * 
 * @author garethf
 */
public class CSSValidatorTask extends Task
{
	
	/** Is the task outputting debug information. */
	private boolean isDebugEnabled;
	
	/** Should the build fail if any warnings are found. */
	private boolean failOnWarning;
	
	/** Should the build fail if any errors are found. */
	private boolean failOnError;
	

	/** A list of CSS profile validation tasks. */
	private final List<CSSProfileValidatorSubTask> tasks = new ArrayList<CSSProfileValidatorSubTask>();

	
	/**
	 * Set whether or not debug mode should be activated.
	 * 
	 * @param isDebugEnabled true, If debug should be enabled, false otherwise.
	 */
	public void setDebugEnabled(final boolean isDebugEnabled)
	{
		this.isDebugEnabled = isDebugEnabled;
	}

	
	/**
	 * Set if the build should fail if an error is encountered.
	 * 
	 * @param failOnError true if the build should fail if any errors are found.
	 */
	public void setFailOnError(final boolean failOnError)
	{
		this.failOnError = failOnError;
	}
	
	
	/**
	 * Set if the build should fail if a warning is encountered.
	 * 
	 * @param failOnWarning true if the build should fail if any warnings are found.
	 */
	public void setFailOnWarning(final boolean failOnWarning)
	{
		this.failOnWarning = failOnWarning;
	}

	
	
    /**
     * Add a <code>CSSProfileValidationTask</code> instance to the task queue.
     * 
     * @param task The prepared task.
     */
    public void addConfiguredProfile(final CSSProfileValidatorSubTask task)
    {
        tasks.add(task);
    }
    
    
    
	/**
	 * {@inheritDoc}
	 */
	public void execute() throws BuildException 
	{
		log("Starting CSS Validation.");
        log("Debug enabled: " + String.valueOf(isDebugEnabled));

		for (final CSSProfileValidatorSubTask task : tasks)
		{
			task.configure(isDebugEnabled, failOnWarning, failOnError);
			task.perform();
		}
		
		log("Finished CSS Validation.");
	}
	
}
